package cn.fourhundredandeighteen.service.impl;

import cn.fourhundredandeighteen.dao.CourseDao;
import cn.fourhundredandeighteen.dao.ScoreDao;
import cn.fourhundredandeighteen.dao.StudentCourseDao;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.Score;
import cn.fourhundredandeighteen.entity.StudentCourse;
import cn.fourhundredandeighteen.service.CourseService;
import cn.fourhundredandeighteen.service.StudentCourseService;
import cn.fourhundredandeighteen.utill.MysqlConnect;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: liuyongqi
 * @time: 2020/12/15
 */
public class CourseServiceImpl implements CourseService {
    @Override
    public List<Course> sleectCourse(List<Long> courseId) {
        CourseDao courseDao= MysqlConnect.into().getMapper(CourseDao.class);
        List<Course> courseList=new ArrayList<>();
        for (Long course:courseId){
            try {
                Course course1 = courseDao.selectByPrimaryKey(course);
                courseList.add(course1);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        MysqlConnect.destroy();
        return courseList;
    }

    @Override
    public List<Course> selectCourse(Long studentId) {
        StudentCourseService studentCourseService=new StudentCourseServiceImpl();
        try {
            List<StudentCourse> studentCourses = studentCourseService.selectStudentToCourse(studentId);
            List<Long> courseId=studentCourses.stream().map(StudentCourse::getCourseId).collect(Collectors.toList());
            CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
            List<Course> courses = courseDao.selectCourse(courseId);
            MysqlConnect.destroy();
            return courses;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int upCourse(Course course) {
        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        try {
            int i = courseDao.updateByPrimaryKeySelective(course);
            MysqlConnect.destroy();
            if (i>0){
                return i;
            }else
                return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public List<Course> getCourseAll() {
        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        try {
            List<Course> courses = courseDao.selectAll();
            MysqlConnect.destroy();
            if (courses!= null){
                return courses;
            }else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int delCourse(Long courseId) {

        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        try {
            int i = courseDao.deleteByPrimaryKey(courseId);
            MysqlConnect.destroy();
            if (1>0){
                return i;
            }else {
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int setCourse(Course course) {
        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        try {
            int i = courseDao.insertSelective(course);
            MysqlConnect.destroy();
            if (i>0){
                return i;
            }else
                return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Score select(Long studentId, Long courseId) {
        ScoreDao scoreDao=MysqlConnect.into().getMapper(ScoreDao.class);
        Score score = scoreDao.selectByStudentScore(studentId, courseId);
        MysqlConnect.destroy();
        if (score!=null)
            return score;
        else
            return null;
    }

    @Override
    public int setScore(Score score) {
        ScoreDao scoreDao=MysqlConnect.into().getMapper(ScoreDao.class);
        Score score1 = scoreDao.selectByStudentScore(score.getStudentId(), score.getCourseId());
        if (score1!=null){
            //更新
            int i = scoreDao.updateByPrimaryKeySelective(score);
            if (i>0)
                return i;
            else
                return 0;
        }else {
            int i = scoreDao.insert(score);
            if (i > 0)
                return i;
            else
                return 0;
        }
    }

    @Override
    public int cancelCourse(Long studentId, Long courseId) {
        StudentCourseDao studentCourseDao=MysqlConnect.into().getMapper(StudentCourseDao.class);
        int i = studentCourseDao.delStudentCourse(courseId, studentId);
        MysqlConnect.destroy();
        System.out.println("test:"+i);
        if (i>0){
            //更新课程人数
            CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
            int i1 = courseDao.updataStudentCount(courseId, -1);
            MysqlConnect.destroy();
            if (i1>0)
                return 1;
            else
                return 0;

        }else {
            return 0;
        }
    }

    @Override
    public Course getCourse(Long courseId) {
        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        Course course = courseDao.selectByPrimaryKey(courseId);
        MysqlConnect.destroy();
        if (course!=null){
            return course;
        }else
            return null;
    }

    @Override
    public Score checkScore(Long studentId, Long courseId) {
        ScoreDao scoreDao=MysqlConnect.into().getMapper(ScoreDao.class);
        Score score = scoreDao.selectByStudentScore(studentId, courseId);
        if (score!=null){
            return score;
        }else {
            return null;
        }
    }

    @Override
    public int upScore(Score score) {
        ScoreDao scoreDao=MysqlConnect.into().getMapper(ScoreDao.class);
        int i = scoreDao.updateByPrimaryKey(score);
        if (i>0){
            return i;
        }else {
            return 0;
        }
    }
}
